//
//  ListenTableViewCell.m
//  SuperNews
//
//  Created by dllo on 16/1/13.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "ListenTableViewCell.h"
#import "SpecialListenCollectionViewCell.h"
#import "ListenTypeModel.h"
#import "RadioModel.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface ListenTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

@implementation ListenTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)setCArr:(NSMutableArray *)cArr
{
    if (_cArr != cArr) {
        _cArr = cArr;
    }
    [self.collectionV reloadData];
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.layout.itemSize = CGSizeMake(WIDTH / 3.0, 200);
    self.layout.minimumLineSpacing = 0;
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200) collectionViewLayout:self.layout];
    self.collectionV.delegate = self;
    self.collectionV.backgroundColor = [UIColor whiteColor];
    self.collectionV.dataSource = self;
    [self.contentView addSubview:self.collectionV];
    
    [self.collectionV registerClass:[SpecialListenCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ListenTypeModel *typeModel = [self.cArr objectAtIndex:indexPath.row];
    RadioModel *radioModel = typeModel.radio;
    SpecialListenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSURL *url = [NSURL URLWithString:radioModel.imgsrc];
    [cell.imageV sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placehodler"]];
    cell.titleLabel.text = radioModel.tname;
    cell.detailLabel.text = radioModel.title;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ListenTypeModel *typeModel = [self.cArr objectAtIndex:indexPath.row];
    RadioModel *radioModel = typeModel.radio;
    [self.delegate listenTableViewCellHandle:radioModel listid:typeModel.tid];
}

@end
